package at.ac.tuwien.vpsbcm.spielzeugfabrik.space.utils;

import java.net.URI;
import java.util.ArrayList;

import org.mozartspaces.capi3.AnyCoordinator;
import org.mozartspaces.capi3.ContainerNameNotAvailableException;
import org.mozartspaces.capi3.Coordinator;
import org.mozartspaces.capi3.FifoCoordinator;
import org.mozartspaces.capi3.LabelCoordinator;
import org.mozartspaces.capi3.LindaCoordinator;
import org.mozartspaces.core.Capi;
import org.mozartspaces.core.ContainerReference;
import org.mozartspaces.core.MzsConstants;
import org.mozartspaces.core.MzsCoreException;

public class ContainerUtils {
	private Capi capi;
	private URI spaceurl;
	
	private ContainerReference BauteilContainer;
	private ContainerReference BauteilVerbautContainer;
	private ContainerReference LokomotivenContainer;
	private ContainerReference LokomotivenGetestetContainer;
	private ContainerReference LokomotivenFehlerhaftContainer;
	private ContainerReference AusgeliefertContainer;
	private ContainerReference ProduzentContainer;
	private ContainerReference AuftragContainer;
	private ContainerReference BenchmarkContainer;
	
	public ContainerUtils (Capi capi, URI spaceurl) {
		this.capi = capi;
		this.spaceurl = spaceurl;
		this.setBauteilContainer(null);
		this.setBauteilVerbautContainer(null);
		this.setLokomotivenContainer(null);
		this.setLokomotivenGetestetContainer(null);
		this.setLokomotivenFehlerhaftContainer(null);
		this.setBenchmarkContainer(null);
	}
	
	public ContainerReference InitBauteileContainer () throws MzsCoreException {
		ArrayList<Coordinator> obligatoryCoords = new ArrayList<Coordinator>();
		ArrayList<Coordinator> optionalCoords = new ArrayList<Coordinator>();
		try {
			obligatoryCoords.add(new LindaCoordinator());
			obligatoryCoords.add(new FifoCoordinator());
			optionalCoords.add(new AnyCoordinator("RAD"));
			optionalCoords.add(new AnyCoordinator("unlackierte"));
			optionalCoords.add(new AnyCoordinator("unlackierte_schornsteine"));
			optionalCoords.add(new AnyCoordinator("unlackierte_gehaeuse"));
			optionalCoords.add(new AnyCoordinator("lackierte_schornsteine"));
			optionalCoords.add(new AnyCoordinator("lackierte_gehaeuse"));
			optionalCoords.add(new LabelCoordinator("llackierte_schornsteine"));
			optionalCoords.add(new LabelCoordinator("llackierte_gehaeuse"));
			BauteilContainer = capi.createContainer("Bauteile", spaceurl, MzsConstants.Container.UNBOUNDED, obligatoryCoords, optionalCoords, null);
		} catch (ContainerNameNotAvailableException e) {
				BauteilContainer = capi.lookupContainer("Bauteile", spaceurl, 0, null);
		}
		return BauteilContainer;
	}
	
	public ContainerReference InitBauteileVerbautContainer () throws MzsCoreException {
		ArrayList<Coordinator> obligatoryCoords = new ArrayList<Coordinator>();
		ArrayList<Coordinator> optionalCoords = new ArrayList<Coordinator>();
		try {
			obligatoryCoords.add(new LindaCoordinator());
			obligatoryCoords.add(new FifoCoordinator());
			obligatoryCoords.add(new LabelCoordinator());
			BauteilVerbautContainer = capi.createContainer("BauteileVerbaut", spaceurl, MzsConstants.Container.UNBOUNDED, obligatoryCoords, optionalCoords, null);
		} catch (ContainerNameNotAvailableException e) {
				BauteilVerbautContainer = capi.lookupContainer("BauteileVerbaut", spaceurl, 0, null);
		}
		return BauteilVerbautContainer;
	}
	
	public ContainerReference InitLokomotivenContainer () throws MzsCoreException {
		ArrayList<Coordinator> obligatoryCoords = new ArrayList<Coordinator>();
		ArrayList<Coordinator> optionalCoords = new ArrayList<Coordinator>();
		try {
			obligatoryCoords.add(new LindaCoordinator());
			obligatoryCoords.add(new FifoCoordinator());
			optionalCoords.add(new FifoCoordinator("unlackierte"));
			optionalCoords.add(new FifoCoordinator("fertige"));
			optionalCoords.add(new LabelCoordinator("auftragfarbe"));
			optionalCoords.add(new AnyCoordinator());
			LokomotivenContainer = capi.createContainer("Lokomotiven", spaceurl, MzsConstants.Container.UNBOUNDED, obligatoryCoords, optionalCoords, null);
		} catch (ContainerNameNotAvailableException e) {
				LokomotivenContainer = capi.lookupContainer("Lokomotiven", spaceurl, 0, null);
		}
		return this.LokomotivenContainer;
	}
	
	public ContainerReference InitLokomotivenGetestetContainer () throws MzsCoreException {
		ArrayList<Coordinator> obligatoryCoords = new ArrayList<Coordinator>();
		ArrayList<Coordinator> optionalCoords = new ArrayList<Coordinator>();
		try {
            obligatoryCoords = new ArrayList<Coordinator>();
			obligatoryCoords.add(new AnyCoordinator());
			obligatoryCoords.add(new FifoCoordinator());
			setLokomotivenGetestetContainer(capi.createContainer("LokomotivenGetestet", spaceurl, MzsConstants.Container.UNBOUNDED, obligatoryCoords, optionalCoords, null));
		} catch (ContainerNameNotAvailableException e) {
				setLokomotivenGetestetContainer(capi.lookupContainer("LokomotivenGetestet", spaceurl, 0, null));
		}
		return this.LokomotivenGetestetContainer;
	}
	
	public ContainerReference InitLokomotivenFehlerhaftContainer () throws MzsCoreException {
		ArrayList<Coordinator> obligatoryCoords = new ArrayList<Coordinator>();
		ArrayList<Coordinator> optionalCoords = new ArrayList<Coordinator>();
		try {
            obligatoryCoords = new ArrayList<Coordinator>();
            optionalCoords.add(new AnyCoordinator("fehlerhaft"));
            optionalCoords.add(new AnyCoordinator("zerlegt"));
			obligatoryCoords.add(new FifoCoordinator());
			setLokomotivenFehlerhaftContainer(capi.createContainer("LokomotivenFehlerhaft", spaceurl, MzsConstants.Container.UNBOUNDED, obligatoryCoords, optionalCoords, null));
		} catch (ContainerNameNotAvailableException e) {
				setLokomotivenFehlerhaftContainer(capi.lookupContainer("LokomotivenFehlerhaft", spaceurl, 0, null));
		}
		return this.LokomotivenFehlerhaftContainer;
	}
	
	public ContainerReference InitAusgeliefertContainer () throws MzsCoreException {
		ArrayList<Coordinator> obligatoryCoords = new ArrayList<Coordinator>();
		ArrayList<Coordinator> optionalCoords = new ArrayList<Coordinator>();
		try {
            obligatoryCoords = new ArrayList<Coordinator>();
			obligatoryCoords.add(new AnyCoordinator());
			obligatoryCoords.add(new FifoCoordinator());
			AusgeliefertContainer = capi.createContainer("Ausgeliefert", spaceurl, MzsConstants.Container.UNBOUNDED, obligatoryCoords, optionalCoords, null);
		} catch (ContainerNameNotAvailableException e) {
			AusgeliefertContainer = capi.lookupContainer("Ausgeliefert", spaceurl, 0, null);
		}
		return AusgeliefertContainer;
	}

	public ContainerReference InitProduzentContainer () throws MzsCoreException {
		ArrayList<Coordinator> obligatoryCoords = new ArrayList<Coordinator>();
		ArrayList<Coordinator> optionalCoords = new ArrayList<Coordinator>();
		try {
            obligatoryCoords = new ArrayList<Coordinator>();
			obligatoryCoords.add(new LabelCoordinator());
			optionalCoords = new ArrayList<Coordinator> ();
			ProduzentContainer = capi.createContainer("Produzent", spaceurl, MzsConstants.Container.UNBOUNDED, obligatoryCoords, optionalCoords, null);
		} catch (ContainerNameNotAvailableException e) {
				ProduzentContainer = capi.lookupContainer("Produzent", spaceurl, 0, null);
		}
		return ProduzentContainer;
	}

	public ContainerReference InitAuftragContainer () throws MzsCoreException {
		ArrayList<Coordinator> obligatoryCoords = new ArrayList<Coordinator>();
		ArrayList<Coordinator> optionalCoords = new ArrayList<Coordinator>();
		try {
            obligatoryCoords = new ArrayList<Coordinator>();
            obligatoryCoords.add(new FifoCoordinator ());
            obligatoryCoords.add(new LabelCoordinator ());
			optionalCoords = new ArrayList<Coordinator> ();
			optionalCoords.add(new AnyCoordinator ("ungebaut"));
			optionalCoords.add(new AnyCoordinator ("unlackiert"));
			optionalCoords.add(new AnyCoordinator ("lackiert"));
			optionalCoords.add(new AnyCoordinator("getestet"));
			optionalCoords.add(new AnyCoordinator("fehlerhaft"));
			AuftragContainer = capi.createContainer("Auftrag", spaceurl, MzsConstants.Container.UNBOUNDED, obligatoryCoords, optionalCoords, null);
		} catch (ContainerNameNotAvailableException e) {
			AuftragContainer = capi.lookupContainer("Auftrag", spaceurl, 0, null);
		}
		return AuftragContainer;
	}
	
	public ContainerReference InitBenchmarkContainer () throws MzsCoreException {
		ArrayList<Coordinator> obligatoryCoords = new ArrayList<Coordinator>();
		ArrayList<Coordinator> optionalCoords = new ArrayList<Coordinator>();
		try {
            obligatoryCoords = new ArrayList<Coordinator>();
			obligatoryCoords.add(new LabelCoordinator());
			optionalCoords = new ArrayList<Coordinator> ();
			BenchmarkContainer = capi.createContainer("Benchmark", spaceurl, MzsConstants.Container.UNBOUNDED, obligatoryCoords, optionalCoords, null);
		} catch (ContainerNameNotAvailableException e) {
				BenchmarkContainer = capi.lookupContainer("Benchmark", spaceurl, 0, null);
		}
		return BenchmarkContainer;
	}
	
	public ContainerReference getBauteilContainer() {
		return BauteilContainer;
	}

	public void setBauteilContainer(ContainerReference bauteilContainer) {
		BauteilContainer = bauteilContainer;
	}

	public ContainerReference getBauteilVerbautContainer() {
		return BauteilVerbautContainer;
	}

	public void setBauteilVerbautContainer(
			ContainerReference bauteilVerbautContainer) {
		BauteilVerbautContainer = bauteilVerbautContainer;
	}

	public ContainerReference getLokomotivenContainer() {
		return LokomotivenContainer;
	}

	public void setLokomotivenContainer(ContainerReference lokomotivenContainer) {
		LokomotivenContainer = lokomotivenContainer;
	}

	public ContainerReference getLokomotivenGetestetContainer() {
		return LokomotivenGetestetContainer;
	}

	public void setLokomotivenGetestetContainer(
			ContainerReference lokomotivenGetestetContainer) {
		LokomotivenGetestetContainer = lokomotivenGetestetContainer;
	}

	public ContainerReference getLokomotivenFehlerhaftContainer() {
		return LokomotivenFehlerhaftContainer;
	}

	public void setLokomotivenFehlerhaftContainer(
			ContainerReference lokomotivenFehlerhaftContainer) {
		LokomotivenFehlerhaftContainer = lokomotivenFehlerhaftContainer;
	}

	public ContainerReference getAusgeliefertContainer() {
		return AusgeliefertContainer;
	}

	public void setAusgeliefertContainer(ContainerReference ausgeliefertContainer) {
		AusgeliefertContainer = ausgeliefertContainer;
	}

	public ContainerReference getBenchmarkContainer() {
		return BenchmarkContainer;
	}

	public void setBenchmarkContainer(ContainerReference benchmarkContainer) {
		BenchmarkContainer = benchmarkContainer;
	}
}
